package com.topnav.mc.admin.dao.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topnav.mc.admin.entity.bean.RoleBean;
import com.topnav.mc.admin.entity.bean.UserBean;
import com.topnav.mc.admin.entity.param.RoleParam;
import com.topnav.mc.admin.entity.param.UserParam;
import com.topnav.mc.base.mc.McMapper;
import org.apache.ibatis.annotations.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.topnav.mc.admin.entity.Role;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RoleMapper extends McMapper<Role, RoleBean, RoleParam> {
    String commSql = " SELECT r.*,p.pmNames " +
            " FROM mc_role r " +
            " LEFT JOIN ( " +
            "    SELECT rp.role_id,GROUP_CONCAT(p.pm_name) AS pmNames  FROM mc_role_permission rp LEFT JOIN mc_permission p ON rp.pm_id = p.id GROUP BY rp.role_id " +
            " )p ON r.id = p.role_id " +
            " ${ew.customSqlSegment}";

    @Select(commSql)
    RoleBean selectInfoOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    List<RoleBean> selectInfoList(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    Page<RoleBean> selectInfoPage(Page<RoleParam> page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

    /**
     * 根据人员和单位查询 级别最高的 角色
     * @param userId
     * @param orgId
     * @return
     */
    Role getOneHighLeveByUserOrgRole(@Param("userId") Long userId,@Param("orgId") Long orgId);

    /**
     * 从用户角色表查询权限级别最
     * 高的角色
     * @param userId
     * @return
     */
    Role getOneHighLevelByUserRole(@Param("userId") Long userId);
}
